# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:张广勤
@Web site: https://www.tunan.wang
@Github:www.github.com
 
@File:chrome_test5_0.py
@Time:2024/11/2 19:09

@Motto:不积跬步无以至千里，不积小流无以成江海！
"""

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException, TimeoutException
import time

def convert_to_seconds(time_str):
    # 分割字符串获取分钟和秒
    parts = time_str.split('分')
    minutes = int(parts[0])  # 将分钟转换为整数
    seconds = int(parts[1].replace('秒', ''))  # 移除"秒"并转换为整数

    # 将分钟转换为秒，并加上秒
    total_seconds = minutes * 60 + seconds
    return total_seconds

# 指定 ChromeDriver 的路径
driver_path = 'd:/code_zgq/ganbu/chromedriver.exe'
service = Service(executable_path=driver_path)

# 创建 WebDriver 实例
driver = webdriver.Chrome(service=service)
driver.implicitly_wait(3)
driver.maximize_window()

# 打开网页
driver.get('https://www.sxgbxx.gov.cn/login')

# 等待页面加载完成
wait = WebDriverWait(driver, 10)

# 找到用户名输入框并输入用户名
username_input = wait.until(EC.presence_of_element_located((By.ID, 'userEmail')))
username_input.send_keys('u0122092')

# 找到密码输入框并输入密码
password_input = wait.until(EC.presence_of_element_located((By.ID, 'userPassword')))
password_input.send_keys('071726zgq001!')

# 提示用户输入验证码
input("按回车继续...")

# 直接跳转到目标页面
driver.get("https://www.sxgbxx.gov.cn/front/playkpoint/870a4f1201e8d51e")

# 获取文章列表
articles = driver.find_elements(By.CSS_SELECTOR, "div.n-ct-of > div div.c-p-wrap")

# 在循环中等待元素出现
for article in articles:
    try:
        # play_icon = article.find_element(By.CSS_SELECTOR, "a.play-icon-box")
        # play_icon.click()

        # 等待新页面加载或元素更新
        # time.sleep(2)  # 给页面一些时间来加载新的内容

        # 再次查找元素以确认是否成功点击
        # play_icon = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "a.play-icon-box")))
        article.find_element(By.CSS_SELECTOR, "a.play-icon-box")
        print('ok',article)
        print(article.get_attribute("outerHTML"))
        if article.get_attribute("title") == "图文播放":
            article.click()
            time.sleep(10)
            print("图文**************************")
        if article.get_attribute("title") == "视频播放":
            article.click()
            time.sleep(20)
            print("视频&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&")
    except (NoSuchElementException, TimeoutException) as e:
        print(f"发生错误: {e}")
        continue  # 跳过当前迭代，继续下一个

# 关闭浏览器
driver.quit()